# -*- coding: utf-8 -*-
import urllib
import sys

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os
import time
#指定数据的输出：图片
class DoubanmoviePipeline(object):

        #构造方法
        def __init__(self):
            #指定图片输出的文件夹
            self.i = 1
            self.folder_name = 'output/images'
            if not os.path.exists(self.folder_name):
                os.mkdir(self.folder_name)
                pass
            pass

        def process_item(self, item, spider):
            #数据处理
            #pic:https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg
            #获取图片的地址
            pic_url = item['Mov_pic'][0]
            file_name = pic_url.split('/')[-1]
            #图片下载
            #urllib.request.url(地址，路径+文件名+后缀名)
            urllib.request.urlretrieve(pic_url,self.folder_name+os.sep+str(self.i))
            self.i = self.i + 1

            return item
